﻿
namespace Asd.MP.Utility
{
    public static class JavaScriptHelper
    {
        /// <summary>
        /// 只创建弹出式警告消息脚本
        /// </summary>
        /// <param name="message">消息正文</param>
        /// <returns>alert(message)</returns>
        public static string Alert(string message)
        {
            return string.IsNullOrEmpty(message) ? string.Empty : string.Format("alert(\"{0}\");", message);
        }
        /// <summary>
        /// 弹出警告消息，然后跳转到指定页面
        /// </summary>
        /// <param name="alertType">弹出式消息类型</param>
        /// <param name="message">消息正文</param>
        /// <param name="operate">针对消息类型要进行的操作</param>
        /// <returns>脚本字符串'</returns>
        public static string Alert(string message, AlertType alertType, string operate)
        {
            string script = string.Empty;
            switch (alertType)
            {
                case AlertType.ClosePage:
                    script = "window.close();";
                    break;
                case AlertType.GoBack:
                    script = string.Format("history.go({0})", operate);
                    break;
                case AlertType.RedirectToURL:
                    script = string.Format("location.href=\"{0}\";", operate);
                    break;
                case AlertType.SpecifiedProcess:
                    script = operate;
                    break;
            }
            return Alert(message) + script;
        }

        public static string Close()
        {
            return "(function(){ var pop = window.open(\"\", \"_self\");pop.close();})();";
        }
    }
    /// <summary>
    /// alert 弹出消息操作类型
    /// <remarks>
    /// MatchZhou - 2009/1/19
    /// </remarks>
    /// </summary>
    public enum AlertType
    {
        /// <summary>
        /// 只弹出消息，不进行其它操作
        /// </summary>
        Information,
        /// <summary>
        /// 弹出消息，并指向指定的URL，例：location.href = 'abc.htm';
        /// </summary>
        RedirectToURL,
        /// <summary>
        /// 弹出消息，并后退指定的步数，例：history.go(-1)
        /// </summary>
        GoBack,
        /// <summary>
        /// 弹出消息，并关闭当前窗口，例：window.close()
        /// </summary>
        ClosePage,
        /// <summary>
        /// 弹出消息，并执行指定的代码的操作，例：parseInt('1');
        /// </summary>
        SpecifiedProcess
    }
}
